package com.whfc.ms.api.hdc.controller;

import com.whfc.common.exception.BizException;
import com.whfc.common.result.PageData;
import com.whfc.common.result.Result;
import com.whfc.common.result.ResultEnum;
import com.whfc.common.result.ResultUtil;
import com.whfc.common.util.AssertUtil;
import com.whfc.emp.service.AppHelmetService;
import com.whfc.hdc.dto.AppDeviceDTO;
import com.whfc.hdc.service.AppDeviceService;
import com.whfc.mach.dto.AppMachDataLogDTO;
import com.whfc.mach.dto.obd.AppDeviceObdDataDTO;
import com.whfc.mach.service.AppMachDataService;
import com.whfc.mach.service.ObdDataService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Date;


/**
 * @Description:硬件中心
 * @author: xugcheng
 * @version: 1.0
 * @date: 2019/8/7 15:52
 */
@RequiresPermissions("hdc")
@RestController
@RequestMapping("ms/api/hdc")
public class AppDeviceController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @DubboReference(interfaceClass = AppDeviceService.class, version = "1.0.0")
    private AppDeviceService commonDeviceService;

    @DubboReference(interfaceClass = ObdDataService.class, version = "1.0.0")
    private ObdDataService obdLogService;

    @DubboReference(interfaceClass = AppMachDataService.class, version = "1.0.0")
    private AppMachDataService appMachDataService;

    @DubboReference(interfaceClass = AppHelmetService.class, version = "1.0.0")
    private AppHelmetService appHelmetService;


    @GetMapping("device/list")
    public Result getDeviceList(@RequestParam("pageNum") Integer pageNum,
                                @RequestParam("pageSize") Integer pageSize,
                                @RequestParam("type") Integer type,
                                @RequestParam(value = "sn", required = false) String sn) {
        logger.info("查询硬件列表,pageNum:{},pageSize:{},type:{},sn:{}", pageNum, pageSize, type, sn);
        PageData<AppDeviceDTO> data = commonDeviceService.getDeviceList(pageNum, pageSize, type, sn);
        return ResultUtil.success(data);
    }

    @GetMapping("device/mach/log")
    public Result getDeviceMachDataLog(@RequestParam("sn") String sn,
                                       @RequestParam("startTime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime,
                                       @RequestParam("endTime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime,
                                       @RequestParam("pageNum") Integer pageNum,
                                       @RequestParam("pageSize") Integer pageSize) {
        logger.info("查询机械硬件数据,sn:{},startTime:{},endTime:{},pageNum:{},pageSize:{}", sn, startTime, endTime, pageNum, pageSize);
        AppDeviceDTO device = commonDeviceService.getDeviceBySn(sn);
        if (device == null) {
            throw new BizException(ResultEnum.NOT_FOUND.getCode(), "设备不存在");
        }
        AssertUtil.isTrue(startTime == null || endTime == null, "请先选择数据时间段！");
        PageData<AppMachDataLogDTO> pageData = appMachDataService.getMachDataLogListByDeviceId(device.getId(), startTime, endTime, pageNum, pageSize);
        return ResultUtil.success(pageData);
    }

    @PostMapping("device/mach/data/update")
    public Result updateDeviceMachData(@RequestParam("sn") String sn,
                                       @RequestParam("time") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date time,
                                       @RequestParam(value = "lng", required = false) Double lng,
                                       @RequestParam(value = "lat", required = false) Double lat,
                                       @RequestParam(value = "location", required = false) String location,
                                       @RequestParam(value = "oilPos", required = false) Integer oilPos) {
        logger.info("修改机械硬件数据,sn:{},time:{},lng:{},lat:{},location:{},oilPos:{}", sn, time, lng, lat, location, oilPos);
        return ResultUtil.success();
    }

    @GetMapping("device/helmet/log")
    public Result getDeviceHelmetLog(@RequestParam("sn") String sn,
                                     @RequestParam("startTime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime,
                                     @RequestParam("endTime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime,
                                     @RequestParam("pageNum") Integer pageNum,
                                     @RequestParam("pageSize") Integer pageSize) {
        logger.info("查询安全帽硬件数据,sn:{},startTime:{},endTime:{},pageNum:{},pageSize:{}", sn, startTime, endTime, pageNum, pageSize);
        PageData data = appHelmetService.getDeviceDataLog(sn, startTime, endTime, pageNum, pageSize);
        AssertUtil.isTrue(startTime == null || endTime == null, "请先选择数据时间段！");
        return ResultUtil.success(data);
    }

    @PostMapping("device/helmet/data/update")
    public Result updateDeviceHelmetData(@RequestParam("sn") String sn,
                                         @RequestParam("time") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date time,
                                         @RequestParam(value = "lng", required = false) Double lng,
                                         @RequestParam(value = "lat", required = false) Double lat,
                                         @RequestParam(value = "location", required = false) String location) {
        logger.info("修改安全帽硬件数据,sn:{},time:{},lng:{},lat:{},location:{}", sn, time, lng, lat, location);
        return ResultUtil.success();
    }



    @GetMapping("device/obd/data/list")
    public Result getDeviceObdDataList(@RequestParam("sn") String sn,
                                       @RequestParam("startTime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime,
                                       @RequestParam("endTime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime,
                                       @RequestParam("pageNum") Integer pageNum,
                                       @RequestParam("pageSize") Integer pageSize) {
        logger.info("查询OBD硬件数据列表,sn:{},startTime:{},endTime:{}", sn, startTime, endTime);
        AppDeviceDTO device = commonDeviceService.getDeviceBySn(sn);
        if (device == null) {
            throw new BizException(ResultEnum.NOT_FOUND.getCode(), "设备不存在");
        }
        PageData data = obdLogService.getDeviceObdDataList(device.getId(), startTime, endTime, pageNum, pageSize);
        return ResultUtil.success(data);
    }

    @GetMapping("device/obd/data/detail")
    public Result getDeviceObdDataDetail(@RequestParam("sn") String sn,
                                         @RequestParam("time") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date time) {
        logger.info("查询OBD硬件数据详情,sn:{},time:{}", sn, time);
        AppDeviceDTO device = commonDeviceService.getDeviceBySn(sn);
        if (device == null) {
            throw new BizException(ResultEnum.NOT_FOUND.getCode(), "设备不存在");
        }
        AppDeviceObdDataDTO data = obdLogService.getDeviceObdDataDetail(device.getId(), time);
        return ResultUtil.success(data);
    }

    @PostMapping("device/obd/data/update")
    public Result updateDeviceObdData(@RequestParam("sn") String sn,
                                      @RequestParam("time") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date time,
                                      @RequestParam(value = "lng", required = false) Double lng,
                                      @RequestParam(value = "lat", required = false) Double lat,
                                      @RequestParam(value = "location", required = false) String location) {
        logger.info("修改安OBD硬件数据,sn:{},time:{},lng:{},lat:{},location:{}", sn, time, lng, lat, location);
        return ResultUtil.success();
    }
}
